package me.xiaoyan.android.widget;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;


import android.os.Environment;

public class DataSave {
	private static final String dataFilePath = AppSetting.APP_SAVE_BASE + "/datastore.db";
	private boolean SDCardExits = true;

	public DataSave() {
		checkSDCard();
	}

	public void saveData(String key, Object value) {
		if (SDCardExits) {
			try {
				DataStore ds = getDataStore();
				FileOutputStream fos = new FileOutputStream(dataFilePath);
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				ds.setData(key, value);
				oos.writeObject(ds);
				oos.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

		}
	}

	public List<?> getList(String key) throws IOException,
			ClassNotFoundException {
		DataStore ds = getDataStore();
		if (ds != null) {
			return ds.getList(key);
		}
		return null;
	}

	public Object getObject(String key) {
		DataStore ds;
		try {
			ds = getDataStore();
			if (ds != null) {
				return ds.getObject(key);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return null;
	}

	public String getString(String key) throws IOException,
			ClassNotFoundException {
		DataStore ds = getDataStore();
		if (ds != null) {
			return ds.getString(key);
		}
		return null;
	}

	public int getInt(String key) throws IOException, ClassNotFoundException {
		DataStore ds = getDataStore();
		if (ds != null) {
			return ds.getInt(key);
		}
		return 0;
	}

	public boolean getBoolean(String key, boolean b) throws IOException,
			ClassNotFoundException {
		DataStore ds = getDataStore();
		if (ds != null) {
			return ds.getBoolean(key, b);
		}
		return b;
	}

	public DataStore getDataStore() throws IOException, ClassNotFoundException {
		if (SDCardExits) {
			DataStore ds = new DataStore();
			File f = new File(dataFilePath);
			if (!f.exists()) {
				FileOutputStream fos = new FileOutputStream(dataFilePath);
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				ds.setString("init_data_file_time",
						"" + System.currentTimeMillis());
				oos.writeObject(ds);
				oos.close();
				fos.close();
				return ds;
			}
			FileInputStream fis = new FileInputStream(dataFilePath);
			ObjectInputStream ois = new ObjectInputStream(fis);
			ds = (DataStore) ois.readObject();
			ois.close();
			fis.close();
			return ds;
		}
		return null;
	}

	private void checkSDCard() {
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			SDCardExits = false;
		}
	}

	public Object getObj(String key) {
		DataStore ds;
		try {
			ds = getDataStore();
			if (ds != null) {
				return ds.getObject(key);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
}
